package com.ktmall.ktmalluijava.service.impl;

import com.ktmall.ktmalluijava.mapper.BrandMapper;
import com.ktmall.ktmalluijava.pojo.Brand;
import com.ktmall.ktmalluijava.service.BrandService;
import com.ktmall.ktmalluijava.utils.Page;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ZhouShuang
 * @version 17
 * @Title
 * @date 2025/5/13 上午11:46
 */
@Service
public class BrandServiceImpl implements BrandService {

    @Resource
    private BrandMapper brandMapper;
    /**
     * 获取分页对象
     *
     * @param name
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public Page<Brand> getPage(String name, Integer pageNo, Integer pageSize) {
        Page<Brand> page = new Page<>();
        page.setPageSize(pageSize);
        page.setTotalCount(brandMapper.count(name));
        page.setPageNo(pageNo);
        int offset = (page.getPageNo()-1)*pageSize;
        page.setData(brandMapper.selectList(name,offset,pageSize));
        return page;
    }

    /**
     * 添加品牌信息
     *
     * @param brand
     * @return
     */
    @Override
    public int add(Brand brand) {
        return brandMapper.add(brand);
    }

    /**
     * 更新品牌信息
     *
     * @param brand
     * @return
     */
    @Override
    public int update(Brand brand) {
        return brandMapper.update(brand);
    }

    /**
     * 品牌详情
     *
     * @param id
     * @return
     */
    @Override
    public Brand detail(Integer id) {
        return brandMapper.detail(id);
    }

    /**
     * 删除品牌
     *
     * @param id
     * @return
     */
    @Override
    public int delete(Integer ...id) {
        if(id.length==1){
            return brandMapper.delete(id[0]);
        }
        int count = 0;
        for (Integer integer : id) {
            count += brandMapper.delete(integer);
        }
        return count;
    }

    /**
     * 获取所有
     *
     * @return
     */
    @Override
    public List<Brand> getAll() {
        return brandMapper.getAll();
    }
}
